/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.querie.Algos;
import com.querie.SI.PutItemData;
import com.querie.SI.PutSessionData;
import com.querie.beans.ItemS;
import com.querie.beans.SessionAll;
import com.querie.Voldemort.VoldemortH;
import com.google.gson.Gson;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.*;


/**
 *
 * @author Naushin
 */
public class TestSimilarity{


   public static void main(String[] args){


      SeparateSimilarity sm= new SeparateSimilarity();
    //  TestSimilarity t=new TestSimilarity();
      Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "querie";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";
    String strSQL1 = "";
    String strSQL2="";



        ResultSet rs0 = null;
       Statement stmt = null;


  try{
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");

           stmt = conn.createStatement();
         strSQL1 = "Select sessionId, itemId, count(itemId) from querie.sessionqueries AS sq left outer join querie.qryitems AS qi on sq.queryId = qi.queryId where sessionId=61462 group by sessionId, itemId";
             rs0 = stmt.executeQuery(strSQL1);
            
            List<Integer> keys = new ArrayList<Integer>();
            List<Integer> values = new ArrayList<Integer>();
                int thisSessionId=0;
             while(rs0.next()){
                thisSessionId=rs0.getInt(1);
               keys.add(rs0.getInt(2));
               values.add(rs0.getInt(3));
             }
           PutSessionData pd1=new PutSessionData();
          pd1.putAll(thisSessionId,keys,values);
          VoldemortH v=new VoldemortH();
             for(int i=0;i<keys.size();i++){
              List<Integer> sessionValue=new ArrayList<Integer>();
              List<Integer> count=new ArrayList<Integer>();
              ItemS it=new ItemS();
              Gson gson1=new Gson();

                 String x=Integer.toString(keys.get(i));
              String item=v.getValue(x, "Item");
         //      System.out.println("item is"+item);
           
              if (item==null)
                  {
                sessionValue.add(thisSessionId);
              count.add(values.get(i));

              PutItemData pd=new PutItemData();

              pd.putAll(keys.get(i), sessionValue, count);
//               it=gson1.fromJson(item, ItemS.class);
              item=v.getValue(x, "Item");
             }

              //try{ item=gson.fromJson(itemLeft, ItemS.class);

              it=gson1.fromJson(item, ItemS.class);
             // System.out.print("it is"+it);
               
              for(int k=0; k<it.getSession().size();k++){
                int sessionName=(it.getSession().get(k).getSessionName());//each session
                int sessionCount=(it.getSession().get(k).getSessionCount());//each corr. count
                sessionValue.add(sessionName);
                count.add(sessionCount);
              }
                 
 
              if(!sessionValue.contains(thisSessionId)){
              sessionValue.add(thisSessionId);
              count.add(values.get(i));
              PutItemData pd=new PutItemData();
              
              pd.putAll(keys.get(i), sessionValue, count);
                 }
             }
       
             conn.close();

             sm.SessionSimilarity(thisSessionId);
              }

    catch(Exception ex){ex.printStackTrace();}
   }



}
